package com.imprenta.bean;

import java.util.ArrayList;


import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.util.JRLoader;
import net.sf.jasperreports.view.JasperViewer;

import com.imprenta.ejb.EjbPedido;
import com.imprenta.helpers.UtilHelper;

import com.imprenta.model.PedidoBase;
import com.imprenta.model.PedidoDataModel;


public class PedidoBean {
	
	/**
	 * @return the lstPedido
	 */
	public ArrayList<PedidoBase> getLstPedido() {
		fillDataTable();
		return lstPedido;
	}
	/**
	 * @param lstPedido the lstPedido to set
	 */
	public void setLstPedido(ArrayList<PedidoBase> lstPedido) {
		this.lstPedido = lstPedido;
	}	
	/**
	 * @return the pedidoBase
	 */
	public PedidoBase getPedidoBase() {
		return pedidoBase;
	}
	/**
	 * @param pedidoBase the pedidoBase to set
	 */
	public void setPedidoBase(PedidoBase pedidoBase) {
		this.pedidoBase = pedidoBase;
		//goDetalle();
		if(this.pedidoBase != null){System.out.println("Desde el set. id = " + this.pedidoBase.getPedidoId());}
		
	}
	/**
	 * @return the pModel
	 */
	public PedidoDataModel getpModel() {
		return pModel;
	}
	/**
	 * @param pModel the pModel to set
	 */
	public void setpModel(PedidoDataModel pModel) {
		this.pModel = pModel;
	}	
	public PedidoBean() {
		// TODO Auto-generated constructor stub
		fillDataTable();
	}
	private void fillDataTable(){
		try {
			this.setLstPedido(EjbPedido.getListaPedido());
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}
	public void redirectReport(){
		UtilHelper.redirectUrl("/report/reporte.xhtml");
	}
	@SuppressWarnings("deprecation")
	public void reporte(){
		JasperReport reporte;
		try {
			reporte = (JasperReport) JRLoader.loadObject(getClass().getResource("/com/test/report/report-pedido.jasper").getPath());
			
			/*******************************************************************************************************/
			
			//createPdf(print);
//			byte[] fichero = JasperRunManager.runReportToPdf(reporte, null, new JRBeanCollectionDataSource(this.getLstPedido()));// JasperExportManager.exportReportToPdf(print);
//			ExternalContext context = FacesContext.getCurrentInstance().getExternalContext();
//			HttpServletResponse hsr = (HttpServletResponse) context.getResponse();
//			hsr.setContentType("application/pdf");
//			//hsr.getOutputStream().write(fichero);
//			hsr.setHeader("Content-disposition", "atachment;filename=\"reporte.pdf\";");
//			hsr.setHeader("Cache-Control", "max-age=30");
//			hsr.setHeader("Pragma", "No-cache");
//			hsr.setDateHeader("Expires", 0);
//			hsr.setContentLength(fichero.length);
//			JRExporter exporter = new JRPdfExporter();
//			exporter.setParameter(JRExporterParameter.JASPER_PRINT, print);			
//			exporter.exportReport();
			
//			hsr.getOutputStream().write(fichero);			
//			hsr.addHeader("Content-disposition", "atachment;filename=reporte.pdf");
//			hsr.setContentLength(fichero.length);

			/*******************************************************************************************************/
			JasperPrint print = JasperFillManager.fillReport(reporte, null, new JRBeanCollectionDataSource(this.getLstPedido()));
			JasperViewer view = new JasperViewer(print,false);
			view.setVisible(true);
		} catch (JRException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}	

	private PedidoBase pedidoBase;
	private PedidoDataModel pModel;
	private ArrayList<PedidoBase> lstPedido;
}
